﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Core.Services;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.MyTable.Repositories;
using syio = System.IO;

namespace VOL.WebApi.Controllers.MyTable.customer_api
{
    [Route("api/customer_tow")]
    [ApiController]
    public class customer_tow : Controller
    {
        /// <summary>
        /// 初始化流程业务流程
        /// </summary>
        /// <param name="ft_id"></param>
        /// <param name="racct"></param>
        /// <param name="rcntr"></param>
        /// <returns></returns>
        [HttpPost, Route("init_flow"), AllowAnonymous]
        public Object init_flow(Guid ft_id, string racct, string rcntr)
        {
            WebResponseContent _webResponse = new WebResponseContent();
            Hashtable rtn_hst = new Hashtable();
           // Console.WriteLine(ft_id);
            // Fl_template_list fl_Template_List = new Fl_template_list();

            //  Fk_flow fk_Flow = new Fk_flow();
            List<Fl_template_list> _Fl_template_list = DBServerProvider.DbContext.Set<Fl_template_list>().Where(w=>w.ft_id == ft_id).ToList();
            using (var db = DBServerProvider.DbContext)
            {
                IQueryable<Fk_flow> _query = Fk_flowRepository.Instance.FindAsIQueryable(t => (t.racct== racct &&t.rcntr==rcntr));
                DbSet<Fk_flow> Fk_flow_all = DBServerProvider.DbContext.Set<Fk_flow>();
                Fk_flow_all.RemoveRange(_query);

                for (int i=0;i<= _Fl_template_list.Count - 1; i++)
                {
                    Fl_template_list fl_Template_List = _Fl_template_list[i];//流程模版
                    Fk_flow fk_Flow = new Fk_flow();//每个业务流程表
                    fk_Flow.bek = fl_Template_List.bek;
                    fk_Flow.stepid = (int)fl_Template_List.stepid;
                    fk_Flow.nextroleid = fl_Template_List.nextroleid;
                    fk_Flow.racct = racct;
                    fk_Flow.rcntr = rcntr;
                    fk_Flow.pxm=fl_Template_List.pxm;
                    fk_Flow.nextstepid = (int)fl_Template_List.nextstepid;
                    fk_Flow.remark = fl_Template_List.remark;
                    fk_Flow.tjsql=fl_Template_List.tjsql;
                    fk_Flow.tj_je = fl_Template_List.tj_je;
                    fk_Flow.node_attr = fl_Template_List.node_attr;
                    fk_Flow.roleid = fl_Template_List.roleid;
                    db.Add(fk_Flow);
                } 
               
                db.SaveChanges();//注意不能放在foreach循环里面
            }



            return Json(_webResponse.OK("保存成功", rtn_hst));

        }
        }
}
